/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package fy.crm.operate;

import fy.rest.AForm_Save_Operator_Rest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 *
 * @author Administrator
 */
public class SISAccountEntryAppletOperate extends AForm_Save_Operator_Rest {

    private static final Log logger = LogFactory.getLog(SISAccountEntryAppletOperate.class);

    private AppletBean bean = new AppletBean();

    @Override
    protected void parseValueExport(String name, String value) {
        if ("客户Id".equals(name)) {
            bean.setAccounId(value);
        } else if ("客户Id".equals(name)) {
            bean.setAccounId(value);
        }
    }

    @Override
    protected boolean save() {
        boolean success = false;

        try {
                       
            
            Document doc = DocumentHelper.parseText("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:cus=\"http://siebel.com/CustomUI\"><soapenv:Header>\n"
                    + "<wsse:Security xmlns:wsse=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">\n"
                    + "<wsse:UsernameToken xmlns:wsu=\"http://schemas.xmlsoap.org/ws/2002/07/utility\">\n"
                    + "<wsse:Username>SADMIN</wsse:Username>\n"
                    + "<wsse:Password Type=\"wsse:PasswordText\">SADMIN</wsse:Password>\n"
                    + "</wsse:UsernameToken>\n"
                    + "</wsse:Security>\n"
                    + "</soapenv:Header>\n"
                    + "   <soapenv:Body>\n"
                    + "      <cus:FYOAApproveServiceAccntApprove_Input>\n"
                    + "         <cus:Accountfinalstatus></cus:Accountfinalstatus>\n"
                    + "         <cus:AccounId></cus:AccounId>\n"
                    + "         <cus:Accountmessage></cus:Accountmessage>\n"
                    + "         <cus:Accountposition></cus:Accountposition>\n"
                    + "         <cus:Oacreateddatetime></cus:Oacreateddatetime>\n"
                    + "      </cus:FYOAApproveServiceAccntApprove_Input>\n"
                    + "   </soapenv:Body>\n"
                    + "</soapenv:Envelope>");
            Element e = (Element) doc.selectNodes("//cus:FYOAApproveServiceAccntApprove_Input").get(0);

            for (Iterator iter = e.elements().iterator(); iter.hasNext();) {
                Element node = (Element) iter.next();
                String nodeName = node.getName();
                if ("Accountfinalstatus".equals(nodeName)) {
                    node.setText(bean.getAccountfinalstatus());
                } else if ("AccounId".equals(nodeName)) {
                    node.setText(bean.getAccounId());
                } else if ("Accountmessage".equals(nodeName)) {
                    node.setText(bean.getAccountmessage());
                } else if ("Accountposition".equals(nodeName)) {
                    node.setText(bean.getAccountposition());
                } else if ("Oacreateddatetime".equals(nodeName)) {
                    node.setText(bean.getOacreateddatetime());
                }
            }

            byte[] buf = doc.asXML().getBytes();

            URL url = new URL("http://10.112.0.200/eai_anon_chs/start.swe?SWEExtSource=SecureWebService&SWEExtCmd=Execute");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setRequestProperty("Content-Length", String.valueOf(buf.length));
            conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            conn.setRequestProperty("SOAPAction", "\"document/http://siebel.com/CustomUI:FYOAApproveServiceAccntApprove\"");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);

            OutputStream os = conn.getOutputStream();
            os.write(buf);
            os.flush();
            os.close();

            System.out.println(new String(buf));

            System.out.println(conn.getResponseCode());

            if (conn.getResponseCode() == 200) {
                InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String result = null;
                while ((result = br.readLine()) != null) {
                    System.out.println(result);
                    logger.info(result);
                }
            }

        } catch (MalformedURLException ex) {
            logger.error("新客户授信审批表数据异常", ex);
        } catch (IOException ex) {
            logger.error("新客户授信审批表数据异常", ex);
        } catch (Exception ex) {
            logger.error("新客户授信审批表数据异常", ex);
        }

        return success;
    }

    public String getFormName() {
        return "新客户授信审批表";
    }

    private class AppletBean {

        private String accounId;
        private String accountfinalstatus;
        private String accountmessage;
        private String accountposition;
        private String oacreateddatetime;

        public String getAccounId() {
            return accounId;
        }

        public void setAccounId(String accounId) {
            this.accounId = accounId;
        }

        public String getAccountfinalstatus() {
            return accountfinalstatus;
        }

        public void setAccountfinalstatus(String accountfinalstatus) {
            this.accountfinalstatus = accountfinalstatus;
        }

        public String getAccountmessage() {
            return accountmessage;
        }

        public void setAccountmessage(String accountmessage) {
            this.accountmessage = accountmessage;
        }

        public String getAccountposition() {
            return accountposition;
        }

        public void setAccountposition(String accountposition) {
            this.accountposition = accountposition;
        }

        public String getOacreateddatetime() {
            return oacreateddatetime;
        }

        public void setOacreateddatetime(String oacreateddatetime) {
            this.oacreateddatetime = oacreateddatetime;
        }

    }

}
